// AWTScrollbar.cpp: implementation of the AWTScrollbar class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "awt.h"
#include "AWTScrollbar.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTScrollbar, AWTComponent)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

AWTScrollbar::AWTScrollbar()
{
	construct(VERTICAL, 0, 10, 0, 100);
}

AWTScrollbar::AWTScrollbar(int orientation)
{
	construct(orientation, 0, 10, 0, 100);
}

AWTScrollbar::AWTScrollbar(int orientation, int value, int visible, int minimum, int maximum)
{
	construct(orientation, value, visible, minimum, maximum);
}

AWTScrollbar::~AWTScrollbar()
{

}

CWnd* AWTScrollbar::createComponent()					 
{
	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	CScrollBar* pwnd = new AWTScrollbarPeer(this);
	DWORD dwStyle;
	if (m_visible)
		dwStyle = WS_CHILD | WS_VISIBLE;
	else
		dwStyle = WS_CHILD;
	if (m_orientation == HORIZONTAL)
		dwStyle |= SBS_HORZ;
	else
		dwStyle |= SBS_VERT;
	pwnd->Create(dwStyle, getBounds(), wParent, 0);
	((AWTScrollbarPeer*)pwnd)->setValues(m_value, m_visibleAmount, m_minimum, m_maximum);
	((AWTScrollbarPeer*)pwnd)->setLineIncrement(m_lineIncrement);
	((AWTScrollbarPeer*)pwnd)->setPageIncrement(m_pageIncrement);
	if (!m_enabled)
		pwnd->EnableWindow(FALSE);
	return pwnd;
}

void AWTScrollbar::setOrientation(int orientation)
{
	if (orientation == m_orientation)
		return;
	m_orientation = orientation;
	if (m_peer != NULL) 
	{
		removeNotify();
		addNotify();
		invalidate();
  }
}

void AWTScrollbar::setLineIncrement(int v) 
{
	m_lineIncrement = v;
	if (m_peer != NULL) 
	{
		AWTScrollbarPeer* peer = (AWTScrollbarPeer*)m_peer;
	  peer->setLineIncrement(v);
	}
}

void AWTScrollbar::setPageIncrement(int v) 
{
	m_pageIncrement = v;
	if (m_peer != NULL) 
	{
		AWTScrollbarPeer* peer = (AWTScrollbarPeer*)m_peer;
	  peer->setPageIncrement(v);
	}
}

void AWTScrollbar::setValues(int value, int visible, int minimum, int maximum) 
{
	if (maximum <= minimum) 
	{
	  maximum = minimum + 1;
	}
	if (visible > maximum - minimum) 
	{
		visible = maximum - minimum;
	}
	if (visible < 1) 
	{
		visible = 1;
	}
	if (value < minimum) 
	{
	  value = minimum;
	}
//	if (value > maximum - visible) 
//	{
//	  value = maximum - visible;
//	}

	m_value = value;
	m_visibleAmount = visible;
	m_minimum = minimum;
	m_maximum = maximum;
	if (m_peer != NULL) 
	{
		AWTScrollbarPeer* peer = (AWTScrollbarPeer*)m_peer;
	  peer->setValues(value, visible, minimum, maximum);
	}
}

CSize AWTScrollbar::getMinimumSize()
{
	return getPreferredSize();
}

CSize AWTScrollbar::getPreferredSize()
{
	if (m_peer == NULL)
		return CSize(0,0);
	CSize sz;
	if (m_orientation == HORIZONTAL)
	{
		sz.cx = (::GetSystemMetrics(SM_CXHSCROLL) * 2) + ::GetSystemMetrics(SM_CYHSCROLL) + 1;
#ifdef UNDER_CE
		sz.cy = ::GetSystemMetrics(SM_CXHSCROLL);
#else
		sz.cy = ::GetSystemMetrics(SM_CXHTHUMB);
#endif
	}
	else
	{
		sz.cy = (::GetSystemMetrics(SM_CYVSCROLL) * 2) + ::GetSystemMetrics(SM_CXVSCROLL) + 1;
#ifdef UNDER_CE
		sz.cx = ::GetSystemMetrics(SM_CYVSCROLL);
#else
		sz.cx = ::GetSystemMetrics(SM_CYVTHUMB);
#endif
	}
	
	return sz;
}

void AWTScrollbar::processAdjustmentEvent(AWTAdjustmentEvent* e) 
{
	m_value = e->getValue();
  if (m_adjustmentListener != NULL) 
	{
    m_adjustmentListener->adjustmentValueChanged(e);
  }
}
